package com.chen.manager.service.impl;

import com.chen.log.annotation.Log;
import com.chen.manager.mapper.SysUserRoleMapper;
import com.chen.manager.service.SysUserRoleService;
import com.chen.model.dto.system.AssignRoleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author cgj
 * @date 2/20/25 7:52 PM
 */
@Service
@Transactional
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Transactional
    @Log(title = "SysUserRole模块",businessType = 1)
    @Override
    public void assign(AssignRoleDto assignRoleDto) {
        //  根据用户id查,该用户已经有的权限 , 之后在统一作新增
        Long userId = assignRoleDto.getUserId();
        sysUserRoleMapper.deleteRoleByUid(userId);
        int i=1/0;
       //给用户新增角色
        List<Long> roleIdList = assignRoleDto.getRoleIdList();
        //这里需要进行拼接 ,比如 用户id 为1, 要给他新增的角色id 为1,2,3 拼接
        //  为  1  1 , 1 2, 1 3,
        roleIdList.forEach(roleId->{
            sysUserRoleMapper.insertAssignRole(userId,roleId);
        });


    }

    @Override
    public List<Long> findRoleByUserId(Long uid) {
        List<Long> sysUserRoles=sysUserRoleMapper.selectRoleByUserId(uid);
        return sysUserRoles;
    }
}
